package {{packageName}};

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.MSSQLServerContainer;

import java.util.concurrent.atomic.AtomicBoolean;

public class MsSQLTestContainerExtension implements BeforeAllCallback {

  private static AtomicBoolean started = new AtomicBoolean(false);

  private static MSSQLServerContainer<?> container = new MSSQLServerContainer<>("{{mssqlDockerImageWithVersion}}")
    .withUrlParam("trustServerCertificate","true")
    .acceptLicense();

    @Override
    public void beforeAll(ExtensionContext extensionContext) throws Exception {
      if (!started.get()) {
        container.start();
        System.setProperty("spring.datasource.url", container.getJdbcUrl());
        System.setProperty("spring.datasource.password", container.getPassword());
        System.setProperty("spring.datasource.username", container.getUsername());
        System.setProperty("spring.datasource.driver-class-name","com.microsoft.sqlserver.jdbc.SQLServerDriver");
        started.set(true);
      }
    }
}
